//
//  LoopView.m
//  testdemo
//
//  Created by zhoubo on 17/3/15.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "LoopView.h"
#import "LoopViewFlowLayout.h"
#import "ZBCollectionViewCell.h"
#import "LoopModel.h"
#import "NewsDetailViewController.h"
#import "News.h"
#import "ViewController.h"


#define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

@interface LoopView()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic,strong) UIPageControl *pageCon;

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong)NSArray* imgNames;

@end

@implementation LoopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame andCount:(NSInteger)count
{
    if (self = [super initWithFrame:frame]) {
        
        self.count = count;
        [self setup];
        
    }
    return self;
}

-(void)setModels:(NSArray *)models
{
    _models = models;
    [self.collectionView reloadData];
}


-(void)setup
{
    UICollectionView* collectionveiw = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:[[LoopViewFlowLayout alloc]init]];
    
    self.collectionView = collectionveiw;
    [self addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.pageCon = [[UIPageControl alloc]init];
    self.pageCon.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageCon.pageIndicatorTintColor = [UIColor grayColor];
    self.pageCon.hidesForSinglePage = YES;
    self.pageCon.numberOfPages = self.count;
    
    
    [self addSubview:self.pageCon];
    
    [self.collectionView registerClass:[ZBCollectionViewCell class] forCellWithReuseIdentifier:@"democell"];
    

    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    
    self.imgNames = @[@"loopViewK8",@"payOnline"];

    
    
//    NSLog(@"%lf",self.collectionView.contentOffset.x/self.collectionView.bounds.size.width);

    
    
    
}

-(void)layoutSubviews
{
    
    [super layoutSubviews];
    
    CGFloat pageConW = self.count*15;
    CGFloat pageConH = 30;
    CGFloat pageConX = (self.bounds.size.width - pageConW)/2;
    CGFloat pageConY = self.bounds.size.height - 18 - pageConH;
    self.pageCon.frame = CGRectMake(pageConX, pageConY, pageConW, pageConH);
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.count * 100;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZBCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"democell" forIndexPath:indexPath];
    
//    cell.contentView.backgroundColor = randomColor;
    
    NSInteger index = indexPath.row%self.count;
//    cell.iconView.image = [UIImage imageNamed:self.imgNames[index]];
    cell.model = self.models[index];
    


    return cell;
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width)%self.count;
    
    if (page == 0) {
        
        page = self.count - (page==0?0:1);
        [self.collectionView setContentOffset:CGPointMake(page*self.collectionView.bounds.size.width, 0)];
    }

//    [self.pageCon setCurrentPage:page];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = (scrollView.contentOffset.x / scrollView.bounds.size.width) ;
    NSInteger pageZ = page*10;
    NSInteger pageZh = pageZ/10;// 页数的整数部分
    NSInteger pageF = pageZ%10; // 页数的小数部分
    
    if (pageF >= 5) {
        [self.pageCon setCurrentPage:(pageZh%self.count+1)%self.count];
    }
    else
    {
        [self.pageCon setCurrentPage:pageZh%self.count];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LoopModel* model = self.models[indexPath.row%self.count];
    
    News* news = nil;
    NewsDetailViewController* detailcon = [[NewsDetailViewController alloc]init];

    if (model) {
        news = [News newsFromLoopModel:model];
        detailcon.news  = news;
    }

    UINavigationController* con = [UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers[0];
    
    [UIView animateWithDuration:0.7 animations:^{
        ViewController* con = [UIApplication sharedApplication].keyWindow.rootViewController;
        con.mainTabbar.alpha = 0;
    }];
    [con pushViewController:detailcon animated:YES];
    
}



@end
